package com.example.studyenglish.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.TextView;

import com.example.studyenglish.R;
import com.example.studyenglish.database.DataAccess;
import com.example.studyenglish.pattern.BookList;
import com.example.studyenglish.pattern.VocabularyList;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Review extends TabActivity implements TabHost.TabContentFactory {
	private int week = 0;
	public ArrayList<VocabularyList> wordlist;
	private ArrayList<String> listShould;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		TabHost th = getTabHost();
		DataAccess data = new DataAccess(this);
		wordlist = data.QueryList("BOOKID ='" + DataAccess.bookID + "'", null);
		BookList book = data.QueryBook("ID ='" + DataAccess.bookID + "'", null)
				.get(0);
		listShould = new ArrayList<String>(wordlist.size());
		for (int i = 0; i < wordlist.size(); i++) {
			if (wordlist.get(i).getShouldReview().equals("1")) {
				listShould.add(wordlist.get(i).getList());
			}
		}
		this.setTitle("复习-" + book.getName());
		th.addTab(th.newTabSpec("shouldreview").setIndicator("该复习的单元")
				.setContent(this));
		th.addTab(th.newTabSpec("alllists").setIndicator("所有的单元")
				.setContent(this));

	}

	@Override
	public View createTabContent(final String tag) {
		// TODO Auto-generated method stub
		final TextView textview = new TextView(this);
		textview.setTextColor(Color.BLACK);
		textview.setBackgroundDrawable(this.getResources().getDrawable(
				R.drawable.main_budget_lv_header));
		final ListView lv = new ListView(this);
		lv.setCacheColorHint(0);
		LinearLayout ll = new LinearLayout(this);
		ll.setOrientation(LinearLayout.VERTICAL);
		if (tag.equals("shouldreview")) {
			textview.setText("   需要进行复习的单元");
			ll.addView(textview);

			lv.setAdapter(new SimpleAdapter(this, getData(tag), R.layout.list1,
					new String[] { "label", "times", "lasttime", "image" },
					new int[] { R.id.label, R.id.times, R.id.lasttime,
							R.id.list1_image }));
			lv.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
                                        final int arg2, long arg3) {
					// TODO Auto-generated method stub
					startReview(arg2, 1);

				}
			});
			lv.setOnItemLongClickListener(new OnItemLongClickListener() {

				@Override
				public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                               final int arg2, long arg3) {
					// TODO Auto-generated method stub
					Dialog dialog = new AlertDialog.Builder(Review.this)
							.setTitle("操作")
							.setItems(new String[] { "标记为已复习" },
									new DialogInterface.OnClickListener() {

										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											// TODO Auto-generated method stub
											if (which == 0) {
												DataAccess data = new DataAccess(
														Review.this);
												VocabularyList labelList = wordlist.get(Integer
														.parseInt(listShould
																.get(arg2)) - 1);
												labelList.setShouldReview("0");
												labelList.setReview_times(String.valueOf((Integer.parseInt(labelList
														.getReview_times()) + 1)));
												Calendar cal = Calendar
														.getInstance();
												SimpleDateFormat f = new SimpleDateFormat(
														"yyyy-MM-dd");
												String date = f.format(cal
														.getTime());
												labelList.setReviewTime(date);
												data.UpdateList(labelList);
												Intent intent = new Intent();
												intent.setClass(Review.this,
														Review.class);
												finish();
												startActivity(intent);
											}
										}

									})
							.setNegativeButton("取消",
									new DialogInterface.OnClickListener() {

										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											// TODO Auto-generated method stub

										}
									}).create();
					dialog.show();
					return false;
				}

			});
			ll.addView(lv);

		}
		if (tag.equals("alllists")) {
			textview.setText("   所有的单元");
			ll.addView(textview);
			lv.setAdapter(new SimpleAdapter(this, getData(tag), R.layout.list2,
					new String[] { "label", "state", "image" }, new int[] {
							R.id.label, R.id.state, R.id.list2_image }));
			lv.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
                                        final int arg2, long arg3) {
					// TODO Auto-generated method stub
					if (wordlist.get(arg2).getLearned().equals("0")) {
						Dialog dialog = new AlertDialog.Builder(Review.this)
								.setTitle("提醒")
								.setMessage(
										"这个单元(单元-" + (arg2 + 1)
												+ ")还没有学习过，要现在学习吗？")
								.setPositiveButton("确定",
										new DialogInterface.OnClickListener() {
											@Override
											public void onClick(
													DialogInterface dialog,
													int whichButton) {
												/*
												 * User clicked OK so do some
												 * stuff
												 */
												Intent intent = new Intent();
												Bundle bundle = new Bundle();
												bundle.putString("list", String
														.valueOf(arg2 + 1));
												intent.setClass(
														Review.this,
														LearningVocabulary.class);
												intent.putExtras(bundle);
												finish();
												startActivity(intent);
											}
										})
								.setNegativeButton("取消",
										new DialogInterface.OnClickListener() {
											@Override
											public void onClick(
													DialogInterface dialog,
													int whichButton) {
												/*
												 * User clicked OK so do some
												 * stuff
												 */
											}
										}).create();
						dialog.show();
					} else if (wordlist.get(arg2).getShouldReview().equals("1")) {
						startReview(arg2 + 1, 2);
					} else if (wordlist.get(arg2).getShouldReview().equals("0")) {
						Dialog dialog = new AlertDialog.Builder(Review.this)
								.setTitle("提醒")
								.setMessage(
										"这个单元(单元-" + (arg2 + 1)
												+ ")暂时不需要进行复习，确定要现在复习吗？")
								.setPositiveButton("确定",
										new DialogInterface.OnClickListener() {
											@Override
											public void onClick(
													DialogInterface dialog,
													int whichButton) {
												/*
												 * User clicked OK so do some
												 * stuff
												 */
												startReview(arg2 + 1, 2);
											}
										})
								.setNegativeButton("取消",
										new DialogInterface.OnClickListener() {
											@Override
											public void onClick(
													DialogInterface dialog,
													int whichButton) {
												/*
												 * User clicked OK so do some
												 * stuff
												 */
											}
										}).create();
						dialog.show();
					}

				}

			});
			ll.addView(lv);
		}

		return ll;
	}

	private List<Map<String, Object>> getData(String tag) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		if (tag.equals("shouldreview")) {

			for (int i = 0; i < wordlist.size(); i++) {
				if (wordlist.get(i).getShouldReview().equals("1")) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("label", " 单元-" + wordlist.get(i).getList());
					map.put("times", "已复习次数："
							+ wordlist.get(i).getReview_times());
					map.put("lasttime", "上次复习时间："
							+ wordlist.get(i).getReviewTime());
					map.put("image", android.R.drawable.btn_star_big_on);
					list.add(map);

				}
			}
		} else if (tag.equals("alllists")) {
			for (int i = 0; i < wordlist.size(); i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("label", "单元-" + wordlist.get(i).getList());
				if (wordlist.get(i).getLearned().equals("0")) {
					map.put("state", "状态：未学习  ");
					map.put("image", android.R.drawable.btn_star_big_off);
				} else if (wordlist.get(i).getShouldReview().equals("1")) {
					map.put("state", "状态：该复习了！ ");
					map.put("image", android.R.drawable.btn_star_big_on);
				} else if (Integer.parseInt(wordlist.get(i).getReview_times()) >= 5) {
					map.put("state", "状态：复习已完成！ ");
					map.put("image", android.R.drawable.btn_star_big_off);
				}

				else {
					map.put("state", "状态：暂不需复习  ");
					map.put("image", android.R.drawable.btn_star_big_off);
				}
				list.add(map);
			}
		}

		return list;
	}

	private void startReview(final int arg2, final int tag) {
		String tag1 = "";
		if (tag == 1) {
			tag1 = listShould.get(arg2);
		} else {
			tag1 = String.valueOf(arg2);
		}
		Dialog dialog = new AlertDialog.Builder(Review.this)
				.setTitle("开始复习：")
				.setMessage("单元-" + tag1)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						/* User clicked OK so do some stuff */
						Intent intent = new Intent();
						Bundle bundle = new Bundle();
						if (tag == 1)
							bundle.putString("list", listShould.get(arg2));
						else
							bundle.putString("list", String.valueOf(arg2));
						intent.setClass(Review.this, ReviewVocabulary.class);
						intent.putExtras(bundle);
						finish();
						startActivity(intent);
					}
				})
				.setNeutralButton("取消", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						/* User clicked OK so do some stuff */
					}
				}).create();
		dialog.show();

	}
}
